package com.ys.data.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.UserCodeVo;
import com.ys.data.auth.entity.UserCode;
import com.ys.data.auth.mapper.UserCodeMapper;
import com.ys.data.auth.service.UserCodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Objects;

/**
* @version: V1.0
* @author: szw
* @className: UserCodeServiceImpl
* @packageName: UserCodeServiceImpl
* @description:  用户中心接口实现
* @data: 2021-1-19 
**/  
@Service
public class UserCodeServiceImpl extends ServiceImpl<UserCodeMapper, UserCode> implements UserCodeService {

    @Resource
    private UserCodeMapper userCodeMapper;

    /**
     * 新增用户中心数据
     * @param userCodeVo
     * @return
     */
    @Transient
    @ApiLogDetail(detail = "鉴权服务-新增用户中心数据",operationType=BaseEnum.INSERT)
    public int saveUserCode(UserCodeVo userCodeVo) {
        if (Objects.isNull(userCodeVo)) {
            throw  new BaseException(BaseEnum.USER_CODE_DATA_IS_NULL);
        }
       try {
           UserCode userCode = new UserCode();
           BeanUtils.copyProperties(userCodeVo, userCode);
           return userCodeMapper.insert(userCode);
       }catch (Exception e){
           throw new BaseException(BaseEnum.SAVE_USER_CODE_DATA_IS_ERROR);
       }
    }

    /**
     * 更新用户中心数据
     * @param userCodeVo
     * @return
     */
    @Override
    @Transient
    @ApiLogDetail(detail = "鉴权服务-更新用户中心数据",operationType=BaseEnum.UPDATE)
    public int updateUserCode(UserCodeVo userCodeVo) {
        if (Objects.isNull(userCodeVo)) {
            throw  new BaseException(BaseEnum.USER_CODE_DATA_IS_NULL);
        }
        try {
            UserCode userCode = new UserCode();
            BeanUtils.copyProperties(userCodeVo, userCode);
            return userCodeMapper.updateById(userCode);
        }catch (Exception e){
            throw new BaseException(BaseEnum.UPDATE_USER_CODE_DATA_IS_ERROR);
        }
    }

    /**
     * 查询用户中心数据
     * @param userCodeVo
     * @return
     */
    @Override
    @ApiLogDetail(detail = "鉴权服务-查询用户中心数据",operationType=BaseEnum.SELECT)
    public UserCodeVo findUserCode(UserCodeVo userCodeVo) {
        if (null == userCodeVo) {
            throw  new BaseException(BaseEnum.FIND_PARAMETER_IS_NULL);
        }
        try {
            QueryWrapper<UserCode> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",userCodeVo.getUserId());
            UserCode userCode =  userCodeMapper.selectOne(queryWrapper);
            BeanUtils.copyProperties(userCode,userCodeVo);
            return userCodeVo;
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_USER_CODE_DATA_IS_ERROR);
        }
    }
}
